<template>
  <div>
    <el-button type="primary" size="default" icon="ele-Plus" @click="toAddSpu"
       :disabled="!category3Id">添加SPU</el-button
    >
    <el-table
      v-loading="loading"
      border
      stripe
      style="margin: 20px 0"
      :data="spuList"
    >
      <el-table-column type="index" width="80" label="序号" />
      <el-table-column label="Spu名称" prop="spuName"></el-table-column>
      <el-table-column label="Spu描述" prop="description"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-button
            type="primary"
            size="small"
            icon="ele-Plus"
            @click="toAddSku(row)"
          ></el-button>
          <el-button
            type="primary"
            size="small"
            icon="ele-Edit"
            @click="toUpdateSpu(row)"
          ></el-button>
          <el-button
            type="info"
            size="small"
            icon="ele-Warning"
            @click="showInfo(row)"
          ></el-button>
          <el-button
            type="danger"
            size="small"
            icon="ele-Delete"
            @click="deleteSpu(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:page-size="pageSize"
      :page-count="pageCount"
      :pager-count="5"
      :page-sizes="[3, 5, 7, 9]"
      v-model:current-page="currentPage"
      layout=" prev, pager, next, sizes"
    />

    <el-dialog v-model="dialogVisible" title="SKU列表" width="70%">
      <el-table border stripe :data="skuInfoList">
        <el-table-column label="名称" prop="skuName"></el-table-column>
        <el-table-column label="价格" prop="price"></el-table-column>
        <el-table-column label="重量" prop="weight"></el-table-column>
        <el-table-column label="默认图片">
          <template v-slot="{ row }">
            <img :src="row.skuDefaultImg" alt="" height="100" />
          </template>
        </el-table-column>
      </el-table>

      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="dialogVisible = false">
            关闭
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { defineComponent } from "vue";
import { reqSkuListBySpuId, type skuInfoListType } from "@/api/sku";

export default defineComponent({
  name: "SpuList",
});
</script>

<script setup lang="ts">
import { ref, watch } from "vue";
import { storeToRefs } from "pinia";
import { ElMessage } from "element-plus";
import {
  reqDeleteSpu,
  reqSpuList,
  type spuItemType,
  type spuListType,
} from "@/api/spu";
import useCategoryStore from "@/stores/category";
//拿到三级分类的信息
const categoryStore = useCategoryStore();
const { category3Id } = storeToRefs(categoryStore);

//定义页码需要的数据
//1.每页数量
const pageSize = ref(3);
//2.总页数
const pageCount = ref(0);
//3.当前页
const currentPage = ref(1);
//4. table是否加载中
const loading = ref(false);

//接收切换type的自定义事件
const emit = defineEmits<{
  (event: "changeType", type: number, row?: spuItemType): void;
}>();
//切换组件的点击回调函数
const toAddSpu = () => {
  emit("changeType", 1);
};
const toAddSku = (row: spuItemType) => {
  emit("changeType", 2, row);
};
const toUpdateSpu = (row: spuItemType) => {
  emit("changeType", 1, row);
};

//定义spu列表的初始值
const spuList = ref<spuListType>([]);

//封装获取spu列表的请求
const getSpuList = async () => {
  try {
    const result = await reqSpuList(
      currentPage.value,
      pageSize.value,
      category3Id.value as number
    );
    //把总页数和列表属性赋值给自己的数据
    spuList.value = result.records;
    pageCount.value = result.pages;

    loading.value = false;
  } catch (e: any) {
    ElMessage.error(e.message);
  }
};
//监听3id的改变，发送请求
watch(
  category3Id,
  (newVal) => {
    if (!newVal) return;
    loading.value = true;
    //发送初始化请求
    getSpuList();
  },
  { immediate: true }
);

//监听当前页和每页条数 重新发送请求
watch([currentPage, pageSize], () => {
  loading.value = true;
  //发送初始化请求
  getSpuList();
});

//控制sku列表弹窗
const dialogVisible = ref(false);
//设置一个值供表格来使用
const skuInfoList = ref<skuInfoListType>([]);

//查看sku详情
const showInfo = async (row: spuItemType) => {
  dialogVisible.value = true;

  //请求数据
  try {
    const result = await reqSkuListBySpuId(row.id as number);
    skuInfoList.value = result;
  } catch (e: any) {
    ElMessage.error(e.message);
  }
};

//删除某个spu的回调函数
const deleteSpu = async (row: spuItemType) => {
  //请求数据
  try {
    await reqDeleteSpu(row.id as number);
    ElMessage.success("删除成功");

    //发送初始化请求
    getSpuList();
  } catch (e: any) {
    ElMessage.error(e.message);
  }
};
</script>

<style scoped></style>
